package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.SupplierBadDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.SupplierBad;
import com.guodu.entity.SupplierBadStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.SupplierBadService;
import com.guodu.vo.SupplierBadQueryVo;
import com.guodu.vo.SupplierBadVo;

@Service("supplierBadService")
public class SupplierBadServiceImpl implements SupplierBadService{
	@Resource
	private SupplierBadDao supplierBadDao;
	@Resource
	private UnitsDao unitsDao;
	@Resource
	private UserInfoDao userInfoDao;

	public int findSizeByVo(SupplierBadQueryVo supplierBadQueryVo, Units units) {
		
		return supplierBadDao.findSizeByVo(supplierBadQueryVo,units);
	}

	public List<SupplierBadVo> findByVo(SupplierBadQueryVo supplierBadQueryVo,
			Units units, int curPage, int pageSize) {
		List<SupplierBadVo> supplierBadVoList = new ArrayList<SupplierBadVo>();
		List<SupplierBad> supplierBadList = supplierBadDao.findByVo(supplierBadQueryVo, units, curPage, pageSize);
		for(int i=0;i<supplierBadList.size();i++){
			SupplierBadVo supplierBadVo = new SupplierBadVo();
			supplierBadVo.setSupplierBad(supplierBadList.get(i));			
			UserInfo userInfo = userInfoDao.findById(supplierBadList.get(i).getCreatePeople());	
			Units unitsInfo = unitsDao.findById(Long.valueOf(supplierBadList.get(i).getUnitsId()));
			Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
			supplierBadVo.setUnitsLevel(createUnits.getUnitsLevel());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				supplierBadVo.setFunitsName(funitsInfo.getUnitsName());
				supplierBadVo.setFunitsId(unitsInfo.getFunitsId());
				supplierBadVo.setUnitsName(unitsInfo.getUnitsName());
				supplierBadVo.setIndustry(unitsInfo.getIndustry());
				supplierBadVo.setUnitsId(unitsInfo.getUnitsId());
			}else{
				supplierBadVo.setFunitsName(unitsInfo.getUnitsName());
				supplierBadVo.setFunitsId(unitsInfo.getUnitsId());
			}
			supplierBadVo.setUserName(userInfo.getUserName());
			supplierBadVoList.add(supplierBadVo);
			
		}	
		return supplierBadVoList;
	}

	public void save(SupplierBad supplierBad) {
		supplierBadDao.save(supplierBad);
		
	}
	public void examine(String ids, String reason, String beforeStatus,
			Long afterStatus, UserInfo userInfo) {
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<SupplierBadStatus> supplierBadStatusList = new ArrayList<SupplierBadStatus>();
		for(int i=0;i<idses.length;i++){
			SupplierBadStatus supplierBadStatus = new SupplierBadStatus();
			supplierBadStatus.setAfterStatus(afterStatus);
			supplierBadStatus.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			supplierBadStatus.setCreatePeople(userInfo.getUserId());
			supplierBadStatus.setBadId(idses[i]);
			supplierBadStatus.setReason(reason);
			supplierBadStatus.setCreateTime(Calendar.getInstance().getTime());
			supplierBadStatus.setStatusId(i+"");
			supplierBadStatusList.add(supplierBadStatus);			
		}
		supplierBadDao.changeStatus(supplierBadStatusList);
		supplierBadDao.examine(ids,afterStatus);
		
	}

	public void delByIds(String[] ids) {
		supplierBadDao.delByIds(ids);
		
	}

	public SupplierBad findById(String id) {
		
		return supplierBadDao.findById(id);
	}

	
	public void update(SupplierBad supplierBad,Long userId) {
		if(supplierBad.getStatus() == 4L || supplierBad.getStatus() == 2L){
			List<SupplierBadStatus> supplierBadStatusList = new ArrayList<SupplierBadStatus>();
			SupplierBadStatus supplierBadStatus = new SupplierBadStatus();
			supplierBadStatus.setAfterStatus(0L);
			supplierBadStatus.setBeforeStatus(supplierBad.getStatus());
			supplierBadStatus.setCreatePeople(userId);
			supplierBadStatus.setBadId(supplierBad.getBadId());
			supplierBadStatus.setReason("");
			supplierBadStatus.setCreateTime(Calendar.getInstance().getTime());
			supplierBadStatus.setStatusId("1");
			supplierBadStatusList.add(supplierBadStatus);
			supplierBadDao.changeStatus(supplierBadStatusList);
		}		
		supplierBad.setStatus(0L);
		supplierBadDao.update(supplierBad);
	}

	
	public List<SupplierBadStatus> findStatus(String id) {
		
		return supplierBadDao.findStatus(id);
	}

	public void deleteById(String id) {
		supplierBadDao.deleteById(id);
		
	}

	
	public List<SupplierBad> downLoad(SupplierBadQueryVo supplierBadQueryVo,Units units) {
		
		return supplierBadDao.downLoad(supplierBadQueryVo,units);
	}

	@Override
	public void examinAll(SupplierBadQueryVo supplierBadQueryVo, Units units) {
		// TODO Auto-generated method stub
		supplierBadDao.examinAll(supplierBadQueryVo, units);
	}
	
}
